package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfAlarmIndicators;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfAlarmIndicatorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;
import java.util.Map;

/**
 * 指标信息-Controller
 * @author zfsn
 * @date 2023-4-4
 */
@Api(tags = "告警配置管理")
@RestController
@RequestMapping("/alarmIndicators")
public class ConfAlarmIndicatorsController {

	@Autowired
	private ConfAlarmIndicatorsService confAlarmIndicatorsService;

	/**
	 * 通过主键查询数据
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id", paramType = "query", required = true, dataType = "Integer")
	})
	public R<ConfAlarmIndicators> queryById(@PathVariable("id") Integer id) {
		return R.ok(confAlarmIndicatorsService.getById(id));
	}


	/******					自定义告警指标配置										***/

	/**
	 * 分页查询
	 * @param confAlarmIndicators 筛选条件
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@ApiOperation(value = "自定义告警指标分页查询", notes = "自定义告警指标分页查询")
	@PostMapping("/customAlarmIndicators")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isEnable", value = "0-启用 1-禁用", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "name", value = "指标名称", paramType = "body", required = false, dataType = "String")
	})
	public R<Page<ConfAlarmIndicators>> listPageByCustomAlarmIndicators(@RequestBody @ApiIgnore ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(2);
		confAlarmIndicators.setIsDel(0);
		return R.ok(confAlarmIndicatorsService.listPage(confAlarmIndicators));
	}

	/**
	 * 新增自定义告警指标数据
	 * @param confAlarmIndicators 实例对象
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@ApiOperation(value = "新增自定义告警指标数据", notes = "新增自定义告警指标数据")
	@SysLog("新增指标信息数据")
	@PostMapping("/customAlarmIndicators/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "指标名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "valueType", value = "指标值类型", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "unit", value = "指标单位", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "objId", value = "指标对象", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "description", value = "指标说明", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "expression", value = "表达式", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "particleId", value = "颗粒度 告警类型为自定义告警时必填，来自字典表", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> saveCustomAlarmIndicators(@RequestBody @ApiIgnore ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(2);
		return confAlarmIndicatorsService.saveConfig(confAlarmIndicators);
	}

	/**
	 * 通过主键更新自定义告警指标数据
	 * @param confAlarmIndicators 实例对象
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@ApiOperation(value = "通过主键更新自定义告警指标数据", notes = "通过主键更新自定义告警指标数据")
	@SysLog("更新指标信息数据")
	@PutMapping("/customAlarmIndicators")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "name", value = "指标名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "valueType", value = "指标值类型", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "unit", value = "指标单位", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "objId", value = "指标对象", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "description", value = "指标说明", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "expression", value = "表达式", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> updateCustomAlarmIndicatorsById(@Validated({UpdateFlag.class, Default.class}) @RequestBody @ApiIgnore ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(2);
		return confAlarmIndicatorsService.updateConfigById(confAlarmIndicators);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除指标信息数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer")
	})
	public R<String> removeByIds(@PathVariable("ids") List<Integer> ids){
		return confAlarmIndicatorsService.removeConfigByIds(ids);
	}


	/**
	 * 是否禁用指标
	 *
	 * @param confAlarmIndicators
	 *
	 * @return 结果
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@PostMapping("/customAlarmIndicators/editIndicatorsEnable")
	@ApiOperation(value = "是否禁用指标", notes = "是否禁用指标")
	public R<String> editIndicatorsEnable(@RequestBody ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(2);
		return confAlarmIndicatorsService.updateIndicatorsEnable(confAlarmIndicators);
	}


	/******					基础告警指标配置										***/

	/**
	 * 分页查询
	 * @param confAlarmIndicators 筛选条件
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiOperation(value = "基础告警指标分页查询", notes = "基础告警指标分页查询")
	@PostMapping("/baseAlarmIndicators")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isEnable", value = "0-启用 1-禁用", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "name", value = "指标名称", paramType = "body", required = false, dataType = "String")
	})
	public R<Page<ConfAlarmIndicators>> listPageBaseAlarmIndicators(@RequestBody @ApiIgnore ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(1);
		confAlarmIndicators.setIsDel(0);
		return R.ok(confAlarmIndicatorsService.listPage(confAlarmIndicators));
	}

	/**
	 * 通过主键更新自定义告警指标数据
	 * @param confAlarmIndicators 实例对象
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiOperation(value = "通过主键更新基础告警指标数据", notes = "通过主键更新基础告警指标数据")
	@SysLog("更新指标信息数据")
	@PutMapping("/baseAlarmIndicators")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "name", value = "指标名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "valueType", value = "指标值类型", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "unit", value = "指标单位", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "description", value = "指标说明", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "code", value = "指标编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "category", value = "所属类型", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> updateBaseAlarmIndicatorsById(@RequestBody ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(1);
		return confAlarmIndicatorsService.updateConfigById(confAlarmIndicators);
	}

	/**
	 * 是否禁用指标
	 *
	 * @param confAlarmIndicators
	 * @return 结果
	 * @Description type = 1, 告警类型为基础告警
	 */
	@PostMapping("/baseAlarmIndicators/editIndicatorsEnable")
	@ApiOperation(value = "是否禁用指标", notes = "是否禁用指标")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isEnable", value = "0-启用 1-禁用", paramType = "body", required = false, dataType = "Integer"),
	})
	public R<String> editBaseIndicatorsEnable(@RequestBody ConfAlarmIndicators confAlarmIndicators) {
		confAlarmIndicators.setType(1);
		return confAlarmIndicatorsService.updateIndicatorsEnable(confAlarmIndicators);
	}

	/**
	 * 获取基础告警的指标和编码映射关系
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiIgnore
	@GetMapping("/getBaseAlarmIndicatorsAndCode")
	public R<List<Map<Integer, Integer>>> getBaseAlarmIndicatorsAndCode() {
		ConfAlarmIndicators confAlarmIndicators = new ConfAlarmIndicators();
		confAlarmIndicators.setType(1);
		return R.ok(confAlarmIndicatorsService.getBaseAlarmIndicatorsAndCode(confAlarmIndicators));
	}

	/**
	 * 通用查询对象
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiOperation(value = "通用查询对象", notes = "通用查询对象")
	@PostMapping("/list")
	public R<List<ConfAlarmIndicators>> list(@RequestBody ConfAlarmIndicators confAlarmIndicators) {
		return R.ok(confAlarmIndicatorsService.list(Wrappers.query(confAlarmIndicators)));
	}

	/**
	 * 根据指标对象值查询指标编码
	 * @param objValue
	 * @return 通用返回对象
	 */
	@GetMapping("/queryCodeList/{objValue}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "objValue", value = "告警对象值", paramType = "path", required = true, dataType = "Integer"),
	})
	public R queryCodeList(@PathVariable("objValue") Integer objValue) {
		return confAlarmIndicatorsService.queryCodeList(objValue);

	}
}
